Control -  Loops Practice


Part A

Create a project (named in the usual way)  and in it a class with a main. 

Part B

 In the same project add a class Account.  Give it an instance variable to store the balance and one to store the interest rate (both doubles)  Assume the interest rate will be a number between 0 and 1.

 Add a toString that returns something like

$4583.02 (interest rate 10%)

if the balance was 4583.02 and the interest was .1  (note that 0.1 is 10%, 0.5 is 50%, 1.0 is 100%, etc, adjust what you print without messing up your value!)

 

If you have bal dollars in an account which gets i% interest, then after one year you will have (i +1)*bal dollars.  After two years you will have (i+1)*(i+1)*bal dollars, after three (i+1)*(i+1)*(i+1)*bal dollars, and so on, so we multiply by (i + 1) once for each year to get the new amount based on the previous year's amount

Add to Account a method

public void showGrowth(int years)

This will allow us to pass in a number for how many years.

In showGrowth, create a local variable to stand for the projected balance after a number of years (hint: at 0 years, the value is just the balance)

Write a for loop that repeats up to the number of years given.  In the loop, multiply the previous projected balance by (interest + 1) to find the new value for the projected balance.  Print each value.

Note that the code in the for loop to do this is very simple.  Don't overthink it.  No credit will be given for using special math methods/exponents; all you need are basic arithmetic operations. 

  (Do not change the balance stored for the account when doing this!!!)

 

In the same main as part A, ask the user for a balance.  Keep asking until they give a positive balance.

Also ask for a percent interest.  Assume that if they want 10% the user will enter 10 not 0.1 . Keep asking until you get a value for percent between 0 and 100.

Set up an Account based on the information given (remember that if the user said interest is 10 they mean .1). 

Call showGrowth for the period of 20 years for this account

We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste

import java.util.Scanner;
As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);
In order to read from the user, we use the Scanner like this:
int x = scan.nextInt(); // read an int
double d = scan.nextDouble(); // read a double
String s = scan.next(); // read a String

 

In your main,

Use a for loop to get 10 numbers from the user.  At the end, report to the user what the largest and smallest numbers entered were.   You can assume that all the numbers are between (inclusive) 0 and 1000, so no number will be smaller than 0 or larger than 1000.

You will need a variable to store the largest number, and a variable to store the smallest. You do not need to store all the numbers, only the largest number and smallest number you have seen so far.  Think about how you could decide whether the number you just got from the user should be the new largest or smallest.

[EC+20] For each number you read in, check that it really is in the right range.  If you get a number outside the range, keep asking until you do get a valid number. (the invalid numbers do NOT count toward your 10, and should NOT have a chance to be the largest or smallest)

[EC+10] When reporting the largest and smallest number, also report what position they were entered at -- for instance the largest might have been entered at position 8 out of 10 and the smallest at 3 out of 10.